package com.foreveross.action.weixin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.SYWebService.vo.PaymentVo;
import com.foreveross.common.CommonUtil;
import com.foreveross.common.Exception.SYHttpclientCallException;
import com.foreveross.service.OrderInfoService;
import com.foreveross.service.PayCostFavorableInfoService;
import com.foreveross.service.TenementCostService;
import com.foreveross.vo.common.ResultVo;
import com.foreveross.weixin.service.PayRefundService;

/**
 * 微信支付
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/weixin/PayUnifiedOrderController")
public class PayUnifiedOrderController {

	private Logger log = Logger.getLogger(PayUnifiedOrderController.class);

	@Resource
	private PayCostFavorableInfoService payCostFavorableInfoService;
	
	@Resource
	private OrderInfoService orderInfoService;
	
	@Resource
	private TenementCostService tenementCostService;
	
	/**
	 * 微信 润优惠缴费
	 * @param request
	 * @param response
	 * @param body 商品或支付单简要描述
	 * @param total_fee 总金额
	 * @param notify_url 接收微信支付异步通知回调地址
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/sendPay")
	public void sendPay(HttpServletRequest request,
			HttpServletResponse response,String body,String total_fee,
			String notify_url,String appId,String mchId,String apiKey,String orderIds) {
		ResultVo resultVo = new ResultVo();
		try {
			resultVo = payCostFavorableInfoService.payCostFavorableInfo(body, total_fee, notify_url, 
					CommonUtil.getAddrIp(request), CommonUtil.getWeixinID(request), 
					appId, mchId,apiKey, orderIds);
			
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("润优惠支付统一下单 查看失败");
			log.error("润优惠支付统一下单 查看失败!", e);
		}
		CommonUtil.PrintWriterResultWithJSON(response, resultVo,null);
	}
	
	
	/**
	 * 润优惠 微信缴费后操作后台
	 * @param request
	 * @param response
	 * @param orderIds
	 * @param nonceStr
	 * @param appId
	 * @param mchId
	 * @param outTradeNo
	 * @param payType
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/savePayCostFavorableInfo")
	public void savePayCostFavorableInfo(HttpServletRequest request,
			HttpServletResponse response, String orderIds,String nonceStr,
			String appId,String mchId,String apiKey,String outTradeNo,String payType) {
		ResultVo resultVo = new ResultVo();
		try {
			
			boolean isTrue = payCostFavorableInfoService.payCostFavorableInfoCallback(orderIds,nonceStr,
					appId,mchId, apiKey, outTradeNo,payType);
			
			resultVo.setSuccess(isTrue);
			
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("润优惠支付记录 录入失败");
			log.error("润优惠支付记录 录入失败!", e);
		}
		CommonUtil.PrintWriterResultWithJSON(response, resultVo,null);
	}
	
	
	/**
	 * 微信 物业缴费
	 * @param request
	 * @param response
	 * @param body 商品或支付单简要描述
	 * @param total_fee 总金额
	 * @param notify_url 接收微信支付异步通知回调地址
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/sendTenementPay")
	public void sendTenementPay(HttpServletRequest request,
			HttpServletResponse response,String body,String total_fee,
			String notify_url,String appId,String mchId,String apiKey,
			String cusID,String orgId,String revID) {
		ResultVo resultVo = new ResultVo();
		try {
			resultVo = tenementCostService.payTenementInfo(body, total_fee, notify_url, 
					CommonUtil.getAddrIp(request), CommonUtil.getWeixinID(request), 
					appId, mchId, apiKey, cusID,orgId,revID);
			
		}catch(SYHttpclientCallException e){
			resultVo.setSuccess(false);
			resultVo.setMessage(e.getMessage());
		}catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("物业缴费支付统一下单 查看失败");
			log.error("物业缴费支付统一下单 查看失败!", e);
		}
		CommonUtil.PrintWriterResultWithJSON(response, resultVo,null);
	}
	
	
	/**
	 * 微信 物业缴费
	 * @param request
	 * @param response
	 * @param body 商品或支付单简要描述
	 * @param total_fee 总金额
	 * @param notify_url 接收微信支付异步通知回调地址
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/sendTenementPayRes")
	public void sendTenementPayRes(HttpServletRequest request,
			HttpServletResponse response,String body,String total_fee,
			String notify_url,String appId,String mchId,String apiKey,
			String resID,String orgId,String revID,String userId) {
		ResultVo resultVo = new ResultVo();
		try {
			resultVo = tenementCostService.payTenementInfoRes(body, total_fee, notify_url, 
					CommonUtil.getAddrIp(request), CommonUtil.getWeixinID(request), 
					appId, mchId, apiKey, resID,orgId,revID,userId);
			
		}catch(SYHttpclientCallException e){
			resultVo.setSuccess(false);
			resultVo.setMessage(e.getMessage());
		}catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("物业缴费支付统一下单 查看失败");
			log.error("物业缴费支付统一下单 查看失败!", e);
		}
		CommonUtil.PrintWriterResultWithJSON(response, resultVo,null);
	}
	
	
	@RequestMapping(method = RequestMethod.POST, value = "/saveTenementInfo")
	public void saveTenementInfo(HttpServletRequest request,
			HttpServletResponse response, String cusID,String resID, 
			String IpItemName,String repYears,String revAbstract,
			String deviceName, String orgId,
			String revID, String revMoney,
			String nonceStr,String appId,String mchId,String apiKey,String outTradeNo,String userId) {
		ResultVo resultVo = new ResultVo();
		try {
			boolean isTrue = tenementCostService.payTenementInfoCallback(cusID,resID, IpItemName,
					repYears,revAbstract,deviceName,orgId, revID,  revMoney,
					nonceStr,appId,mchId, apiKey, outTradeNo, userId);
			
			resultVo.setSuccess(isTrue);
		}catch(SYHttpclientCallException e){
			resultVo.setSuccess(false);
			resultVo.setMessage(e.getMessage());
		}catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("物业缴费支付记录 录入失败");
			log.error("物业缴费支付记录 录入失败!", e);
		}
		CommonUtil.PrintWriterResultWithJSON(response, resultVo,null);
	}
	
	
	@RequestMapping(method = RequestMethod.POST, value = "/payRefund")
	public void payRefund(HttpServletRequest request,
			HttpServletResponse response, String appId,String mchId,String apiKey,String certUrl,
			String outTradeNo, String total_fee) {
		ResultVo resultVo = new ResultVo();
		try {
			PayRefundService.getPayRefundResult(appId, mchId, apiKey, certUrl, outTradeNo, total_fee);
			
			resultVo.setSuccess(true);
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("退款申请 失败");
			log.error("退款申请  失败!", e);
		}
		CommonUtil.PrintWriterResultWithJSON(response, resultVo,null);
	}
	
}
